<html>
<head>
<title>Friends Update App</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
</head>
<body>

<?php 

require '../facebook-php-sdk/src/facebook.php';
require 'connectionfile.php';

// create application instance
$facebook = new Facebook(array(
	'appId' => '107856579288360',
	'secret' => '866226142830561f05c581686d12b68f',
	'cookie' => true,
));

$session = $facebook->getSession();

if (!empty($session)){
    try{
        $uid = $facebook->getUser();
        $user = $facebook->api('/me');
		$fwends = $facebook->api('me/friends');
    }catch (Exception $e){}


	
	
	
    if (!empty($user)){   
        //if($_GET['installed'] == '1')
        //{
            //header("Location: http://apps.facebook.com/markomeara_firstapp");
        //}
       // header("Location: http://apps.facebook.com/markomeara_firstapp/");
		echo "Hello, ".$user['first_name'] . "<br />";
		echo "This <b>new</b> app will let you find out who has deleted you since the last time you used the app!";
		echo "<br />" . "It's a work in progress, should be done soon..." . "<br />";
		$length = sizeof($fwends[data]);
		echo "In the mean time, here's all <b>" . $length . "</b> of your friends that this app has detected.." . "<br /><br />";
		echo "I've sorted them alphabetically for you!<br /><br />";
		/*Sort alphabethically*/
		//Loop from start
		$j;
		$k;
		for($k = $length; $k >= 0; $k--){
			for($j = 0; $j < $length - 1; $j++){
			
				/*Can't really remember why this works, but it does..*/
				for($t = 0; $t < sizeof($fwends[data][$j]['name']) && $t < sizeof($fwends[data][$j+1]['name']) && $fwends[data][$j]['name'] == $fwends[data][$j+1]['name']; $t++){
				}
			
				if($t < sizeof($fwends[data][$j]['name']) && $t < sizeof($fwends[data][$j+1]['name']) && $fwends[data][$j]['name'] > $fwends[data][$j+1]['name']){
					$temp = $fwends[data][$j];
					$fwends[data][$j] = $fwends[data][$j+1];
					$fwends[data][$j+1] = $temp;
				}
			}
		}
		echo "Your User ID: " . $user['id'] . "<br />";
		//     echo "Session: ".$user;
		
		echo "stored friends: ";
		
		$q_pf = "SELECT * FROM Connections WHERE Deleted = 0 AND UserID = '" .$uid. "' ORDER BY FriendID;";
		$q_pd = "SELECT * FROM Connections WHERE Deleted = 1 AND UserID = '" .$uid. "' ORDER BY FriendID;";

		$prev_friends = mysql_query($q_pf);
		$prev_deleted = mysql_query($q_pd);
		$curr_friends = $fwends[data];
		
		//Find recently deleted friends
		
		while($row = mysql_fetch_array($prev_friends)){
			$i = 0;
			for($i = 0; $i < $length; $i++){
		
				if($row['FriendID'] == $curr_friends[$i]['id']){
					break;
				}
			}
			//If the friend was deleted
			if($i >= $length){
				echo "<p>" .$row['FriendID']. " Marked as DELETED<p><br />";
				$q = "UPDATE Connections SET Deleted = b'1', UpdateDate = NOW() WHERE UserID = '" .$uid. "' AND FriendID = '" .$row['FriendID']. "';";
				$res = mysql_query($q);
			
			}
		}
		
		//Save recently added friends
		$i = 0;
		for($i = 0; $i < $length; $i++){
			
			$found = 'False';
			$del = 'False';
			
			while($found == 'False' && $row = mysql_fetch_array($prev_friends)){
			
				if($curr_friends[$i]['id'] == $row['FriendID']){
					if($row['deleted'] != 0){
						$del = 'True';
					}
					$found = 'True';
				}

			}
			echo "<br /> <br /> <b>Coming soon... </b>";
	/* TO UNCOMMENT THIS		
			//If friend is new
			if($found == 'False'){
			
				$q = "INSERT INTO Connections (UserID, FriendID, FriendName) VALUES (" .$uid. "," .$curr_friends[$i]['id']. "," .$curr_friends[$i]['name']. ");";
				echo $q . "<br />";
				//$res = mysql_query($q);
			
			}
			else if($del == 'True'){
			
				$q = "UPDATE Connections SET Deleted = b'0' WHERE UserID = " .$uid. " AND FriendID = " .$curr_friends[$i]['id']. ")";
				echo $q ."<br />";
			}
	*/
		}
		
/*		echo "ARRAY: <br />";

		echo "\n<table>\n";
		
		while($row = mysql_fetch_array($res1)){
			echo "<tr><td>" .$row['FriendID']. " " .$row['FriendName']. "</td></tr>";
		}
*/		
		echo "\n<table>\n";
		
		echo "<tr><td>" .$res1[1]. "</td></tr>";
		echo "\n</table>\n";
		
/*		echo "\n<table>\n";
		for($i = 0; $i < $length; $i++){
			
			echo "<tr><td>";
			echo $fwends[data][$i]['name'];
			echo "</td><td>";
			echo $fwends[data][$i]['id'];
			echo "</td></tr>\n";
		}
		echo "</table>";
		//print_r($fwends);
		*/
    }
 
 
	else{
        die("Error occurred");
		echo "Exception:... ".$e;
	}
}
else{
	$url = $facebook->getLoginUrl();
	//GOTO($url);
	echo "<a href ='" . $url . "'>Click</a>";
}

mysql_close($dbc);
?>
</body>
</html>